<!DOCTYPE html>
<html lang="zh" class="no-js">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="description" content="fusion-docs">
<meta name="generator" content="Paradox, paradox-material-theme=0.6.0, mkdocs-material=3.0.3">

<meta name="lang:clipboard.copy" content="Copy to clipboard">
<meta name="lang:clipboard.copied" content="Copied to clipboard">
<meta name="lang:search.language" content="">
<meta name="lang:search.pipeline.stopwords" content="true">
<meta name="lang:search.pipeline.trimmer" content="true">
<meta name="lang:search.result.none" content="No matching documents">
<meta name="lang:search.result.one" content="1 matching document">
<meta name="lang:search.result.other" content="# matching documents">
<meta name="lang:search.tokenizer" content="[\s\-]+">


<meta name="description" content="fusion-docs">
<link rel="shortcut icon" href="../assets/images/favicon.png">
<title>日志 · Akka Fusion</title>
<link rel="stylesheet" href="../assets/stylesheets/application.451f80e5.css">
<link rel="stylesheet" href="../assets/stylesheets/application-palette.22915126.css">
<meta name="theme-color" content="#3f51b5" />
<link rel="stylesheet" href="../lib/material__tabs/dist/mdc.tabs.min.css">
<link rel="stylesheet" href="../lib/prettify/prettify.css">
<script src="../assets/javascripts/modernizr.1aa3b519.js"></script>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,400i,700|Roboto+Mono">
<style>
body,input{font-family:"Roboto","Helvetica Neue",Helvetica,Arial,sans-serif}
code,kbd,pre{font-family:"Roboto Mono","Courier New",Courier,monospace}
</style>
<link rel="stylesheet" href="../assets/fonts/font-awesome.css">
<link rel="stylesheet" href="../assets/fonts/material-icons.css">
<link rel="stylesheet" href="../assets/stylesheets/paradox-material-theme.css">
</head>
<body
data-md-color-primary="indigo"
data-md-color-accent="red"
>
<input class="md-toggle" data-md-toggle="drawer" type="checkbox" id="__drawer" autocomplete="off">
<input class="md-toggle" data-md-toggle="search" type="checkbox" id="__search" autocomplete="off">
<label class="md-overlay" data-md-component="overlay" for="__drawer"></label>
<header class="md-header" data-md-component="header">
<nav class="md-header-nav md-grid">
<div class="md-flex">
<div class="md-flex__cell md-flex__cell--shrink">
<a href="../index.html" title="Akka Fusion" class="md-header-nav__button md-logo">
<i class="md-icon">local_library</i>
</a>
</div>
<div class="md-flex__cell md-flex__cell--shrink">
<label class="md-icon md-icon--menu md-header-nav__button" for="__drawer"></label>
</div>
<div class="md-flex__cell md-flex__cell--stretch">
<div class="md-flex__ellipsis md-header-nav__title" data-md-component="title">
<span class="md-header-nav__topic">
Akka Fusion
</span>
<span class="md-header-nav__topic">
日志
</span>
</div>
</div>
<div class="md-flex__cell md-flex__cell--shrink">
<label class="md-icon md-icon--search md-header-nav__button" for="__search"></label>
<div class="md-search" data-md-component="search" role="dialog">
<label class="md-search__overlay" for="__search"></label>
<div class="md-search__inner" role="search">
<form class="md-search__form" name="search">
<input type="text" class="md-search__input" name="query" placeholder="Search" autocapitalize="off" autocorrect="off" autocomplete="off" spellcheck="false" data-md-component="query" data-md-state="active">
<label class="md-icon md-search__icon" for="__search"></label>
<button type="reset" class="md-icon md-search__icon" data-md-component="reset" tabindex="-1">&#xE5CD;</button>
</form>
<div class="md-search__output">
<div class="md-search__scrollwrap" data-md-scrollfix>
<div class="md-search-result" data-md-component="result">
<div class="md-search-result__meta">
Type to start searching
</div>
<ol class="md-search-result__list"></ol>
</div>
</div>
</div>
</div>
</div>

</div>
<div class="md-flex__cell md-flex__cell--shrink">
<div class="md-header-nav__source">
<a href="https://github.com/akka-fusion/akka-fusion"
title="Go to repository"
class="md-source"
data-md-source="github">
<div class="md-source__icon">
<i class="fa fa-github"></i>
</div>
<div class="md-source__repository">
akka-fusion/akka-fusion
</div>
</a>

</div>
</div>
</div>
</nav>
</header>

<div class="md-container">
<main class="md-main">
<div class="md-main__inner md-grid" data-md-component="container">
<div class="md-sidebar md-sidebar--primary" data-md-component="navigation">
<div class="md-sidebar__scrollwrap">
<div class="md-sidebar__inner">
<nav class="md-nav md-nav--primary" data-md-level="0" style="visibility: hidden">
<label class="md-nav__title md-nav__title--site" for="drawer">
<a href="../index.html" title="Akka Fusion" class="md-nav__button md-logo">
<span class="md-nav__button md-logo">
<i class="md-icon">local_library</i>
</a>
<a href="../index.html" title="Akka Fusion">
Akka Fusion
</a>
</label>
<div class="md-nav__source">
<a href="https://github.com/akka-fusion/akka-fusion"
title="Go to repository"
class="md-source"
data-md-source="github">
<div class="md-source__icon">
<i class="fa fa-github"></i>
</div>
<div class="md-source__repository">
akka-fusion/akka-fusion
</div>
</a>

</div>
<ul>
  <li><a href="../intro/index.html" class="page">简介</a></li>
  <li><a href="../core/index.html" class="page">核心</a>
  <ul>
    <li><a href="../core/akka-extension.html" class="page">Akka Extension（扩展）</a></li>
    <li><a href="../core/http-route.html" class="page">Http Route（异步处理器）</a></li>
    <li><a href="../core/fusion-core.html" class="page">FusionCore</a></li>
  </ul></li>
  <li><a href="../http/index.html" class="page">HTTP</a>
  <ul>
    <li><a href="../http/getting.html" class="page">开始</a></li>
    <li><a href="../http/management.html" class="page">管理API</a></li>
    <li><a href="../http/interceptor-chain.html" class="page">Interceptor Chain（拦截器链）</a></li>
  </ul></li>
  <li><a href="../discovery-client/index.html" class="page">服务注册、发现（Client）</a>
  <ul>
    <li><a href="../discovery-client/nacos.html" class="page">连接 Nacos</a></li>
  </ul></li>
  <li><a href="../http-gateway/index.html" class="page">HTTP 代理网关</a></li>
  <li><a href="../jdbc/index.html" class="page">Jdbc</a></li>
  <li><a href="../slick/index.html" class="page">Slick</a></li>
  <li><a href="../mybatis/index.html" class="page">MyBatis</a>
  <ul>
    <li><a href="../mybatis/getting.html" class="page">起步</a></li>
    <li><a href="../mybatis/configuration.html" class="page">配置</a></li>
  </ul></li>
  <li><a href="../data-mongodb/index.html" class="page">Data Mongodb</a></li>
  <li><a href="../data-kafka/index.html" class="page">Data Kafka</a>
  <ul>
    <li><a href="../data-kafka/getting.html" class="page">开始</a></li>
    <li><a href="../data-kafka/producer.html" class="page">Kafka Producer</a></li>
    <li><a href="../data-kafka/consumer.html" class="page">Kafka Consumer</a></li>
  </ul></li>
  <li><a href="../inject/index.html" class="page">依赖管理</a>
  <ul>
    <li><a href="../inject/guice.html" class="page">使用 Guice 进行类依赖管理</a></li>
  </ul></li>
  <li><a href="../log/index.html" class="active page">日志</a></li>
  <li><a href="../configuration/index.html" class="page">配置</a>
  <ul>
    <li><a href="../configuration/core.html" class="page">Fusion Core 配置</a></li>
    <li><a href="../configuration/http.html" class="page">Fusion HTTP 配置</a></li>
    <li><a href="../configuration/http-gateway.html" class="page">Fusion HTTP Gateway 配置</a></li>
    <li><a href="../configuration/jdbc.html" class="page">Fusion JDBC 配置</a></li>
    <li><a href="../configuration/slick.html" class="page">Fusion Slick 配置</a></li>
    <li><a href="../configuration/mybatis.html" class="page">Fusion Mybatis 配置</a></li>
    <li><a href="../configuration/mongodb.html" class="page">Fusion Mongodb 配置</a></li>
    <li><a href="../configuration/kafka.html" class="page">Fusion Kafka 配置</a></li>
  </ul></li>
  <li><a href="../reactivemanifesto.html" class="page">反应式宣言 2.0</a></li>
</ul>
<nav class="md-nav md-nav--secondary">
<label class="md-nav__title" for="__toc">Table of contents</label>
<ul>
  <li><a href="../log/index.html#日志" class="header">日志</a>
  <ul>
    <li><a href="../log/index.html#输出日志到终端" class="header">输出日志到终端</a></li>
    <li><a href="../log/index.html#输出日志到文件" class="header">输出日志到文件</a></li>
    <li><a href="../log/index.html#在-scala-java-应用中使用" class="header">在 Scala/Java 应用中使用</a></li>
    <li><a href="../log/index.html#在-akka-应用中使用" class="header">在 Akka 应用中使用</a></li>
    <li><a href="../log/index.html#在-spring-spring-cloud-中使用" class="header">在 Spring/ Spring Cloud 中使用</a></li>
    <li><a href="../log/index.html#自定义-encoder" class="header">自定义 encoder</a></li>
    <li><a href="../log/index.html#预置配置" class="header">预置配置</a></li>
    <li><a href="../log/index.html#filebeat-配置" class="header">Filebeat 配置</a></li>
  </ul></li>
</ul>
</nav>

</nav>
<ul style="display: none">
<li class="md-nav__item md-version" id="project.version">
<label class="md-nav__link" for="__version">
<i class="md-icon" title="Version">label_outline</i> 2.0.6
</label>
</li>
</ul>
</div>
</div>
</div>
<div class="md-sidebar md-sidebar--secondary" data-md-component="toc">
<div class="md-sidebar__scrollwrap">
<div class="md-sidebar__inner">
<nav class="md-nav md-nav--secondary">
<label class="md-nav__title" for="__toc">Table of contents</label>
<ul>
  <li><a href="../log/index.html#日志" class="header">日志</a>
  <ul>
    <li><a href="../log/index.html#输出日志到终端" class="header">输出日志到终端</a></li>
    <li><a href="../log/index.html#输出日志到文件" class="header">输出日志到文件</a></li>
    <li><a href="../log/index.html#在-scala-java-应用中使用" class="header">在 Scala/Java 应用中使用</a></li>
    <li><a href="../log/index.html#在-akka-应用中使用" class="header">在 Akka 应用中使用</a></li>
    <li><a href="../log/index.html#在-spring-spring-cloud-中使用" class="header">在 Spring/ Spring Cloud 中使用</a></li>
    <li><a href="../log/index.html#自定义-encoder" class="header">自定义 encoder</a></li>
    <li><a href="../log/index.html#预置配置" class="header">预置配置</a></li>
    <li><a href="../log/index.html#filebeat-配置" class="header">Filebeat 配置</a></li>
  </ul></li>
</ul>
</nav>

</div>
</div>
</div>
<div class="md-content">
<article class="md-content__inner md-typeset">
<div class="md-content__searchable">
<h1><a href="#日志" name="日志" class="anchor"><span class="anchor-link"></span></a>日志</h1>
<p>使用 <code>fusion-log</code> 库，需要添加以下依赖：</p><dl class="dependency"><dt>sbt</dt><dd><pre class="prettyprint"><code class="language-scala">libraryDependencies += "com.helloscala.fusion" %% "fusion-log" % "2.0.6"</code></pre></dd><dt>Maven</dt><dd><pre class="prettyprint"><code class="language-xml">&lt;dependency&gt;
  &lt;groupId&gt;com.helloscala.fusion&lt;/groupId&gt;
  &lt;artifactId&gt;fusion-log_2.12&lt;/artifactId&gt;
  &lt;version&gt;2.0.6&lt;/version&gt;
&lt;/dependency&gt;</code></pre></dd><dt>Gradle</dt><dd><pre class="prettyprint"><code class="language-gradle">dependencies {
  compile group: 'com.helloscala.fusion', name: 'fusion-log_2.12', version: '2.0.6'
}</code></pre></dd></dl>
<p>应用开发、运行时，日志作为调试、留痕的重要工具非常重要，Akka Fusion 对此提供了开箱即用的支持。</p>
<ol>
  <li>预置的日志 encoder 配置</li>
  <li>通过 <code>filebeat</code> 输出日志到 Elasticsearch</li>
  <li>良好的自定义支持</li>
</ol>
<h2><a href="#输出日志到终端" name="输出日志到终端" class="anchor"><span class="anchor-link"></span></a>输出日志到终端</h2>
<p><code>logback.xml</code> 日志文件内容：</p>
<pre class="prettyprint"><a class="icon go-to-source" href="https://github.com/akka-fusion/akka-fusion/tree/master/fusion-log/example/logback-stdout.xml" target="_blank" title="Go to snippet source"></a><code class="language-xml">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;configuration scan=&quot;false&quot;&gt;
    &lt;include resource=&quot;fusion/log/logback/defaults.xml&quot;/&gt;
    &lt;include resource=&quot;fusion/log/logback/stdout-appender.xml&quot;/&gt;

    &lt;logger name=&quot;ch.qos&quot; level=&quot;WARN&quot;/&gt;
    &lt;logger name=&quot;com.zaxxer&quot; level=&quot;WARN&quot;/&gt;
    &lt;logger name=&quot;fusion&quot; level=&quot;DEBUG&quot;/&gt;
    &lt;logger name=&quot;helloscala&quot; level=&quot;DEBUG&quot;/&gt;

    &lt;root level=&quot;INFO&quot;&gt;
        &lt;appender-ref ref=&quot;STDOUT&quot;/&gt;
    &lt;/root&gt;
&lt;/configuration&gt;</code></pre>
<h2><a href="#输出日志到文件" name="输出日志到文件" class="anchor"><span class="anchor-link"></span></a>输出日志到文件</h2>
<p><code>logback.xml</code> 日志文件内容：</p>
<pre class="prettyprint"><a class="icon go-to-source" href="https://github.com/akka-fusion/akka-fusion/tree/master/fusion-log/example/logback-file.xml" target="_blank" title="Go to snippet source"></a><code class="language-xml">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;configuration scan=&quot;false&quot;&gt;
    &lt;property name=&quot;fusion.log.logback.file&quot; value=&quot;fusion-example&quot;/&gt;
    &lt;property name=&quot;fusion.log.logback.dir&quot; value=&quot;${user.home}/logs&quot;/&gt;

    &lt;include resource=&quot;fusion/log/logback/defaults.xml&quot;/&gt;
    &lt;include resource=&quot;fusion/log/logback/file-appender.xml&quot;/&gt;
    &lt;include resource=&quot;fusion/log/logback/stdout-appender.xml&quot;/&gt;

    &lt;logger name=&quot;ch.qos&quot; level=&quot;WARN&quot;/&gt;
    &lt;logger name=&quot;com.zaxxer&quot; level=&quot;WARN&quot;/&gt;
    &lt;logger name=&quot;fusion&quot; level=&quot;DEBUG&quot;/&gt;
    &lt;logger name=&quot;helloscala&quot; level=&quot;DEBUG&quot;/&gt;

    &lt;root level=&quot;INFO&quot;&gt;
        &lt;!-- &lt;appender-ref ref=&quot;STDOUT&quot;/&gt; --&gt;
        &lt;appender-ref ref=&quot;FILE&quot;/&gt;
    &lt;/root&gt;
&lt;/configuration&gt;</code></pre>
<table>
  <thead>
    <tr>
      <th>属性 </th>
      <th>说明 </th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><code>fusion.log.logback.dir</code> </td>
      <td>指定输出日志文件名 </td>
    </tr>
    <tr>
      <td><code>fusion.log.logback.file</code> </td>
      <td>指定输出日志保存目录 </td>
    </tr>
  </tbody>
</table>
<p>默认提供的 <code>FILE</code> logback 日志 <strong>Appender</strong> 为了方便 filebeat 读取并存储日志到 Elasticsearch，使用JSON格式输出日志。<a href="#file-appender-xml">file-appender.xml</a> 可以查阅详细的配置及说明。</p>
<h3><a href="#异步输出到文件" name="异步输出到文件" class="anchor"><span class="anchor-link"></span></a>异步输出到文件</h3>
<p>添加一个 <code>AsyncAppender</code> 来包装 <code>RollingFileAppender</code>，即可以异步的方式写文件写入日志。</p>
<pre class="prettyprint"><code class="language-xml">    &lt;appender name=&quot;ASYNC_FILE&quot; class=&quot;ch.qos.logback.classic.AsyncAppender&quot;&gt;
        &lt;queueSize&gt;1024&lt;/queueSize&gt;
        &lt;neverBlock&gt;true&lt;/neverBlock&gt;
        &lt;appender-ref ref=&quot;FILE&quot; /&gt;
    &lt;/appender&gt;

    &lt;root level=&quot;INFO&quot;&gt;
        &lt;appender-ref ref=&quot;ASYNC_FILE&quot;/&gt;
    &lt;/root&gt;
</code></pre>
<h2><a href="#在-scala-java-应用中使用" name="在-scala-java-应用中使用" class="anchor"><span class="anchor-link"></span></a>在 Scala/Java 应用中使用</h2>
<p>应用日志使用 logback 输出，可由程序启动命令参数指定日志配置文件路径。如：<code>-Dlogback.configurationFile=${__APP_PATH__}/logback.xml</code>。</p>
<h3><a href="#scala-logging" name="scala-logging" class="anchor"><span class="anchor-link"></span></a>scala-logging</h3>
<p>在 Scala 应用中，可以使用 <code>StrictLogging</code> 或 <code>LazyLogging</code> 来引入 <code>Logger</code> 变量，这个特性由 <a href="https://github.com/lightbend/scala-logging">scala-logging</a> 库提供。</p>
<pre class="prettyprint"><code class="language-scala">class MyClass extends StrictLogging {
  logger.debug(s&quot;Some $expensive message!&quot;)
  
  logger.whenDebugEnabled {
    println(&quot;This would only execute when the debug level is enabled.&quot;)
    (1 to 10).foreach(x =&gt; println(&quot;Scala logging is great!&quot;))
  }
}
</code></pre>
<p>可以看到，在代码里简单的 extends/with <code>StrictLogging</code> 这个 trait，就可以直接使用 <code>logger</code> 变量来调用它上面的各种日志方法。另外，也不在需要在日志消息里面使用 <code>{}</code> 来作为占位符来输出变量，可直接使用 Scala 的字符串插值特性。scala-logging 基于 Scala macros 提供了编译时扩展：</p>
<pre class="prettyprint"><code class="language-scala">logger.debug(s&quot;Some $expensive message!&quot;)
</code></pre>
<p>将在编译时被替换为：</p>
<pre class="prettyprint"><code class="language-scala">if (logger.isDebugEnabled) logger.debug(s&quot;Some $expensive message!&quot;)
</code></pre>
<h2><a href="#在-akka-应用中使用" name="在-akka-应用中使用" class="anchor"><span class="anchor-link"></span></a>在 Akka 应用中使用</h2>
<p>Akka 有自己的日志级别配置项。所以，最好将 Akka 的日志级别配置与 slf4j 的日志级别保持一致，可以在 HOCON 里面通过以下配置设置 Akka 的日志级别：</p>
<pre class="prettyprint"><code class="language-hocon">akka.loglevel = &quot;DEBUG&quot;
</code></pre>
<p>在 actor 中，可以通过 <code>ActorContext[T]</code> 上提供的 <code>log</code> 方法来使用 Akka 日志，它将使用 Akka 定义的日志组织</p>
<p>当 <code>akka-actor-typed</code> 和 <code>akka-slf4j</code> 存在于类依赖路径上时，Akka 的事件日志处理 actor 将向 SLF4J 发送事件，并自动启用 <code>akka.event.slf4j.Slf4jLogger</code> 和 <code>akka.event.slf4j.Slf4jLoggingFilter</code> 类，而无需要任何配置。若需要手动配置 Akka 使用 SLF4J 输出日志，请确保如下配置，否则将使用默认日志输出到终端。</p>
<pre class="prettyprint"><code class="language-hocon">akka {
  loglevel = &quot;DEBUG&quot;
  loggers = [&quot;akka.event.slf4j.Slf4jLogger&quot;]
  logging-filter = &quot;akka.event.slf4j.Slf4jLoggingFilter&quot;
}
</code></pre><div class="callout note "><div class="callout-title">Tip</div>
<p>使用 actor 的过程中，死信消息会在 <strong>INFO</strong> 级别输出，通常情况下这都是正常的业务状态，可以通过配置抑制这类日志消息在输出几次后被关闭的输出以免干扰我们正常的日志内容。另外，在 ActorSystem 被关闭（<code>terminate</code>）时，actor 邮箱里被挂起的消息将被发送的死信邮箱，我们可以通过配置禁止在 <strong>terminate</strong> 期间输出死信日志。</p>
<pre class="prettyprint"><code class="language-hocon">akka {
  log-dead-letters = 10
  log-dead-letters-during-shutdown = on
}
</code></pre></div>
<h2><a href="#在-spring-spring-cloud-中使用" name="在-spring-spring-cloud-中使用" class="anchor"><span class="anchor-link"></span></a>在 Spring/ Spring Cloud 中使用</h2>
<p>Spring 应用中需要删除<code>logback-spring.xml</code>文件而使用<code>logback.xml</code>（如果存在）。</p>
<p>Spring 应用需要禁用 <code>LoggingSystem</code>，使用此命令行参数可禁用它：<code>-Dorg.springframework.boot.logging.LoggingSystem=none</code>。<a href="https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-custom-log-configuration">点此访问自定义 Spring 日志的更多介绍</a>。</p>
<p>Fusion Log 为日志提供了兼容 Spring Cloud 的参数以在日志输出中输出 Spring应用服务名、启动环境（模式）、IP地址、网络端口等信息。详细映射参数见： <a href="index.html#defaults-xml">#default-xml</a></p>
<h2><a href="#自定义-encoder" name="自定义-encoder" class="anchor"><span class="anchor-link"></span></a>自定义 encoder</h2>
<p>对于默认的 <code>STDOUT</code> 和 <code>FILE</code> 日志格式不满意的，可以自定义自己的日志编码格式。</p>
<pre class="prettyprint"><code class="language-xml">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;configuration scan=&quot;false&quot;&gt;
    &lt;include resource=&quot;fusion/log/logback/defaults.xml&quot;/&gt;
    &lt;include resource=&quot;fusion/log/logback/stdout-appender.xml&quot;/&gt;

    &lt;appender name=&quot;STDOUT_CUSTOM&quot; class=&quot;ch.qos.logback.core.ConsoleAppender&quot;&gt;
        &lt;encoder&gt;
            &lt;pattern&gt;-%d %-5level %fusionEnv %fusionServiceName %fusionServerHost %fusionServerPort [%thread] %logger{36} %line - %msg%n%exception&lt;/pattern&gt;
        &lt;/encoder&gt;
    &lt;/appender&gt;

    &lt;root level=&quot;DEBUG&quot;&gt;
        &lt;appender-ref ref=&quot;console_custom&quot;/&gt;
    &lt;/root&gt;
&lt;/configuration&gt;
</code></pre>
<h2><a href="#预置配置" name="预置配置" class="anchor"><span class="anchor-link"></span></a>预置配置</h2>
<h3><a href="#defaults-xml" name="defaults-xml" class="anchor"><span class="anchor-link"></span></a>defaults.xml</h3>
<pre class="prettyprint"><a class="icon go-to-source" href="https://github.com/akka-fusion/akka-fusion/tree/master/fusion-log/src/main/resources/fusion/log/logback/defaults.xml" target="_blank" title="Go to snippet source"></a><code class="language-xml">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;included&gt;
    &lt;conversionRule conversionWord=&quot;fusionServerHost&quot; converterClass=&quot;fusion.log.logback.LogHostConverter&quot;/&gt;
    &lt;conversionRule conversionWord=&quot;fusionServerIp&quot; converterClass=&quot;fusion.log.logback.LogHostConverter&quot;/&gt;
    &lt;conversionRule conversionWord=&quot;fusionServerPort&quot; converterClass=&quot;fusion.log.logback.LogPortConverter&quot;/&gt;
    &lt;conversionRule conversionWord=&quot;fusionServerHostName&quot; converterClass=&quot;fusion.log.logback.LogHostNameConverter&quot;/&gt;
    &lt;conversionRule conversionWord=&quot;fusionServiceName&quot; converterClass=&quot;fusion.log.logback.LogServiceNameConverter&quot;/&gt;
    &lt;conversionRule conversionWord=&quot;fusionEnv&quot; converterClass=&quot;fusion.log.logback.LogEnvConverter&quot;/&gt;
&lt;/included&gt;</code></pre>
<p>Fusion Log 预定义了几个转换规则参数，可以在 Appender 的编码模式（encoder pattern）里通过 <code>%</code> 参数引用。这几个转换规则参数都将从Java应用Properties变量里查找所需的值。</p>
<ol>
  <li><code>fusionServerHostName</code>：应用服务所在主机名，用以下方式读取：<code>InetAddress.getLocalHost.getCanonicalHostName</code></li>
  <li><code>fusionServerIp</code>、<code>fusionServerHost</code>：应用服务绑定 IP 地址，查找顺序：
    <ul>
      <li><code>-Dfusion.http.default.server.host</code></li>
      <li><code>-Dhttp.host</code></li>
      <li><code>-Dserver.host</code></li>
    </ul>
  </li>
  <li><code>fusionServerPort</code>：应用服务绑定网络端口，查找顺序：
    <ul>
      <li><code>-Dfusion.http.default.server.port</code></li>
      <li><code>-Dhttp.port</code></li>
      <li><code>-Dserver.port</code></li>
    </ul>
  </li>
  <li><code>fusionServerName</code>：应用服务名字，查找顺序：
    <ul>
      <li><code>-Dfusion.name</code></li>
      <li><code>-Dspring.application.name</code></li>
      <li><code>-Dfusion.service.name</code></li>
    </ul>
  </li>
  <li><code>fusionEnv</code>：应用服务启动环境（模式），如：<code>prod</code>、<code>test</code>、<code>dev</code>等运行环境，查找顺序：
    <ul>
      <li><code>-Dfusion.profiles.active</code></li>
      <li><code>-Dspring.profiles.active</code></li>
      <li><code>-Drun.env</code></li>
    </ul>
  </li>
</ol>
<h3><a href="#stdout-appender-xml" name="stdout-appender-xml" class="anchor"><span class="anchor-link"></span></a>stdout-appender.xml</h3>
<pre class="prettyprint"><a class="icon go-to-source" href="https://github.com/akka-fusion/akka-fusion/tree/master/fusion-log/src/main/resources/fusion/log/logback/stdout-appender.xml" target="_blank" title="Go to snippet source"></a><code class="language-xml">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;included&gt;
    &lt;appender name=&quot;STDOUT&quot; class=&quot;ch.qos.logback.core.ConsoleAppender&quot;&gt;
        &lt;encoder&gt;
            &lt;pattern&gt;${fusion.log.logback.console-pattern:-%date{ISO8601} %-5level %thread %logger %X{akkaSource} %line - %msg%n%exception}&lt;/pattern&gt;
        &lt;/encoder&gt;
    &lt;/appender&gt;
&lt;!--    &lt;appender name=&quot;ASYNC_STDOUT&quot; class=&quot;ch.qos.logback.classic.AsyncAppender&quot;&gt;--&gt;
&lt;!--        &lt;queueSize&gt;1024&lt;/queueSize&gt;--&gt;
&lt;!--        &lt;neverBlock&gt;true&lt;/neverBlock&gt;--&gt;
&lt;!--        &lt;appender-ref ref=&quot;STDOUT&quot; /&gt;--&gt;
&lt;!--    &lt;/appender&gt;--&gt;
&lt;/included&gt;</code></pre>
<h3><a href="#file-appender-xml" name="file-appender-xml" class="anchor"><span class="anchor-link"></span></a>　file-appender.xml</h3>
<pre class="prettyprint"><a class="icon go-to-source" href="https://github.com/akka-fusion/akka-fusion/tree/master/fusion-log/src/main/resources/fusion/log/logback/file-appender.xml" target="_blank" title="Go to snippet source"></a><code class="language-xml">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;included&gt;
    &lt;appender name=&quot;FILE&quot; class=&quot;ch.qos.logback.core.rolling.RollingFileAppender&quot;&gt;
        &lt;File&gt;${fusion.log.logback.dir:-${user.dir}/logs}/${fusion.log.logback.file:-${spring.application.name:-fusion}}.log&lt;/File&gt;
        &lt;rollingPolicy class=&quot;ch.qos.logback.core.rolling.TimeBasedRollingPolicy&quot;&gt;
            &lt;FileNamePattern&gt;${fusion.log.logback.dir:-${user.dir}/logs}/${fusion.log.logback.file:-${spring.application.name:-fusion}}.%d{yyyy-MM-dd}.gz&lt;/FileNamePattern&gt;
            &lt;!--只保留最近7天的日志 --&gt;
            &lt;maxHistory&gt;7&lt;/maxHistory&gt;
            &lt;!--用来指定日志文件的上限大小，如果到了这个值，就会删除旧的日志 --&gt;
            &lt;totalSizeCap&gt;8GB&lt;/totalSizeCap&gt;
        &lt;/rollingPolicy&gt;
        &lt;encoder class=&quot;net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder&quot;&gt;
            &lt;charset&gt;UTF-8&lt;/charset&gt;
            &lt;providers&gt;
                &lt;mdc/&gt;
                &lt;timestamp/&gt;
                &lt;pattern&gt;
                    &lt;pattern&gt;
                    {
                        &quot;level&quot;: &quot;%level&quot;,
                        &quot;serviceName&quot;: &quot;%fusionServiceName&quot;,
                        &quot;env&quot;: &quot;%fusionEnv&quot;,
                        &quot;thread&quot;: &quot;%thread&quot;,
                        &quot;logger&quot;: &quot;%logger&quot;,
                        &quot;akkaSource&quot;: &quot;%X{akkaSource}&quot;,
                        &quot;message&quot;: &quot;%level [%thread] %logger %line -\n%message&quot;,
                        &quot;server&quot;: {
                            &quot;host&quot;: &quot;%fusionServerHost&quot;,
                            &quot;port&quot;: &quot;#asLong{%fusionServerPort}&quot;
                        },
                        &quot;exception&quot;: &quot;%exception&quot;
                    }
                    &lt;/pattern&gt;
                &lt;/pattern&gt;
                &lt;callerData/&gt;
                &lt;version/&gt;
                &lt;!-- Printing StackTrace has an import on performance. --&gt;
                &lt;!--&lt;stackTrace/&gt;--&gt;
            &lt;/providers&gt;
        &lt;/encoder&gt;
    &lt;/appender&gt;
&lt;/included&gt;</code></pre>
<p><em>通过 <code>LoggingEventCompositeJsonEncoder</code> 提供了 JSON 格式日志输出支持，它是 logstash 提供的一个 logback encoder 和 appender 库，在此可以查询更多详细：<a href="https://github.com/logstash/logstash-logback-encoder">https://github.com/logstash/logstash-logback-encoder</a> 。</em></p>
<h2><a href="#filebeat-配置" name="filebeat-配置" class="anchor"><span class="anchor-link"></span></a>Filebeat 配置</h2>
<p><strong>filebeat.yml 参考配置如：</strong></p>
<pre class="prettyprint"><a class="icon go-to-source" href="https://github.com/akka-fusion/akka-fusion/tree/master/fusion-log/example/filebeat.yml" target="_blank" title="Go to snippet source"></a><code class="language-yml">filebeat.inputs:
  - type: log

    enabled: true
    json:
      keys_under_root: true
      overwrite_keys: true

    paths:
      - /home/yangjing/logs/*.log

    exclude_files: [&#39;.gz$&#39;]

#============================= Filebeat modules ===============================

filebeat.config.modules:
  path: ${path.config}/modules.d/*.yml
  reload.enabled: false

#==================== Elasticsearch template setting ==========================
setup.kibana:
  host: &quot;localhost:5601&quot;
  #space.id:

#-------------------------- Elasticsearch output ------------------------------
output.elasticsearch:
  hosts: [&quot;localhost:9200&quot;]
  # Optional protocol and basic auth credentials.
  #protocol: &quot;https&quot;
  #username: &quot;elastic&quot;
  #password: &quot;changeme&quot;

#================================ Processors =====================================
# Configure processors to enhance or manipulate events generated by the beat.
processors:
  - add_host_metadata: ~
  - add_cloud_metadata: ~</code></pre>
<p><em>注意是以下三行配置，使filebeat支持json格式日志文件</em></p>
<pre><code>  json:
    keys_under_root: true
    overwrite_keys: true
</code></pre>
</div>
<div>
<a href="https://github.com/akka-fusion/akka-fusion/tree/master/fusion-docs/src/main/paradox/log/index.md" title="Edit this page" class="md-source-file md-edit">
Edit this page
</a>
</div>
<div class="print-only">
<span class="md-source-file md-version">
2.0.6
</span>
</div>
</article>
</div>
</div>
</main>
<footer class="md-footer">
<div class="md-footer-nav">
<nav class="md-footer-nav__inner md-grid">
<a href="../inject/guice.html" title="使用 Guice 进行类依赖管理" class="md-flex md-footer-nav__link md-footer-nav__link--prev" rel="prev">
<div class="md-flex__cell md-flex__cell--shrink">
<i class="md-icon md-icon--arrow-back md-footer-nav__button"></i>
</div>
<div class="md-flex__cell md-flex__cell--stretch md-footer-nav__title">
<span class="md-flex__ellipsis">
<span class="md-footer-nav__direction">
Previous
</span>
使用 Guice 进行类依赖管理
</span>
</div>
</a>
<a href="../configuration/index.html" title="配置" class="md-flex md-footer-nav__link md-footer-nav__link--next" rel="next">
<div class="md-flex__cell md-flex__cell--stretch md-footer-nav__title">
<span class="md-flex__ellipsis">
<span class="md-footer-nav__direction">
Next
</span>
配置
</span>
</div>
<div class="md-flex__cell md-flex__cell--shrink">
<i class="md-icon md-icon--arrow-forward md-footer-nav__button"></i>
</div>
</a>
</nav>
</div>
<div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid">
<div class="md-footer-copyright">
Powered by
<a href="https://github.com/lightbend/paradox">Paradox</a>
and
<a href="https://jonas.github.io/paradox-material-theme/">Paradox Material Theme</a>

</div>
<div class="md-footer-social">
<a href="https://akka-fusion.github.io/akka-fusion/" class="md-footer-social__link fa fa-globe"></a><a href="https://github.com/akka-fusion" class="md-footer-social__link fa fa-github"></a><a href="https://weibo.com/yangbajing" class="md-footer-social__link fa fa-globe"></a>
</div>

</div>
</div>
</footer>

</div>
<script src="../assets/javascripts/application.583bbe55.js"></script>
<script src="../assets/javascripts/paradox-material-theme.js"></script>
<script>app.initialize({version:"0.17",url:{base:"../."}})</script>
<script type="text/javascript" src="../lib/prettify/prettify.js"></script>
<script type="text/javascript" src="../lib/prettify/lang-scala.js"></script>
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function(event) {
window.prettyPrint && prettyPrint();
});
</script>
</body>
</html>
